/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package storymotion.storytelling;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import storymotion.dao.DAO;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class StorytellingDAO extends DAO {

    public StorytellingDAO() throws SQLException {
    }

    public long getNewId() throws SQLException {
        long result = -1;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT nextval('storytelling_id_seq') AS id;");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                result = resultSet.getLong("id");
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return result;
    }

    public void insert(Storytelling storytelling) throws SQLException {
        if (storytelling == null || storytelling.getName() == null || storytelling.getDate() == null || storytelling.getUserId() == null ||  storytelling.getFileName() == null || storytelling.getName().trim().length() == 0 || storytelling.getFileName().trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            String sqlWithId = "INSERT INTO storytelling (id, name, description, date, user_id, file_name) VALUES (?, ?, ?, ?, ?, ?)";
            String sqlWithoutId = "INSERT INTO storytelling (name, description, date, user_id, file_name) VALUES (?, ?, ?, ?, ?)";
            PreparedStatement preparedStatement = null;
            if (storytelling.getId() != null) {
                preparedStatement = connection.prepareStatement(sqlWithId);
                preparedStatement.setLong(1, storytelling.getId());
                preparedStatement.setString(2, storytelling.getName());
                preparedStatement.setString(3, storytelling.getDescription());
                preparedStatement.setTimestamp(4, new java.sql.Timestamp(storytelling.getDate().getTime()));
                preparedStatement.setLong(5, storytelling.getUserId());
                preparedStatement.setString(6, storytelling.getFileName());
            } else {
                preparedStatement = connection.prepareStatement(sqlWithoutId);
                preparedStatement.setString(1, storytelling.getName());
                preparedStatement.setString(2, storytelling.getDescription());
                preparedStatement.setTimestamp(3, new java.sql.Timestamp(storytelling.getDate().getTime()));
                preparedStatement.setLong(4, storytelling.getUserId());
                preparedStatement.setString(5, storytelling.getFileName());
            }
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public void update(Storytelling storytelling) throws SQLException {
        if (storytelling == null || storytelling.getId() == null || storytelling.getName() == null || storytelling.getDate() == null || storytelling.getUserId() == null || storytelling.getFileName() == null || storytelling.getName().trim().length() == 0|| storytelling.getFileName().trim().length() == 0) {
            throw new SQLException("Could not perform this action");
        }
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE storytelling SET name = ?, description = ?, date = ?, user_id = ?, file_name = ? WHERE id = ?");
            preparedStatement.setString(1, storytelling.getName());
            preparedStatement.setString(2, storytelling.getDescription());
            preparedStatement.setTimestamp(3, new java.sql.Timestamp(storytelling.getDate().getTime()));
            preparedStatement.setLong(4, storytelling.getUserId());
            preparedStatement.setString(5, storytelling.getFileName());            
            preparedStatement.setLong(6, storytelling.getId());
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public void delete(long id) throws SQLException {
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM storytelling WHERE id = ?");
            preparedStatement.setLong(1, id);
            int result = preparedStatement.executeUpdate();
            if (result != 1) {
                throw new SQLException("Could not perform this action");
            }
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
    }

    public Storytelling getById(long id) throws SQLException {
        Storytelling storytelling = null;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name, description, date, user_id, file_name FROM storytelling WHERE id = ?");
            preparedStatement.setLong(1, id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                storytelling = new Storytelling();
                storytelling.setId(resultSet.getLong("id"));
                storytelling.setName(resultSet.getString("name"));
                storytelling.setDescription(resultSet.getString("description"));
                storytelling.setDate(new Date(resultSet.getTimestamp("date").getTime()));
                storytelling.setUserId(resultSet.getLong("user_id"));
                storytelling.setFileName(resultSet.getString("file_name"));
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return storytelling;
    }

    public List<Storytelling> getByUserId(long userId) throws SQLException {
        if (userId < 1) {
            throw new SQLException("Could not perform this action");
        }
        List<Storytelling> result = new ArrayList<Storytelling>();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("SELECT id, name, description, date, user_id, file_name FROM storytelling WHERE user_id = ?");
            preparedStatement.setLong(1, userId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Storytelling storytelling = new Storytelling();
                storytelling.setId(resultSet.getLong("id"));
                storytelling.setName(resultSet.getString("name"));
                storytelling.setDescription(resultSet.getString("description"));
                storytelling.setDate(new Date(resultSet.getTimestamp("date").getTime()));
                storytelling.setUserId(resultSet.getLong("user_id"));
                storytelling.setFileName(resultSet.getString("file_name"));
                result.add(storytelling);
            }
            resultSet.close();
            preparedStatement.close();
        } catch (SQLException ex) {
            throw ex;
        }
        return result;
    }

}